/**
 * @Created by zhuly8 zhuly8@yusys.com.cn on 2019-8-2 09:59:16.
 * @updated by
 * @description 公共参数选择器
 */
(function (vue, $, name) {

/*

 <el-row :gutter="20">
          <el-col :span="14">
            <el-select clearable v-model="showObjectFlag" placeholder="对象类型" @change="change">
              <el-option v-for="item in typeOptions" :key="item.key" :value="item.key"
                :label="item.value+'('+item.key+')'"></el-option>
            </el-select>
          </el-col>
          <el-col :span="10">
            <el-button icon="yx-copy" @click="copyInfoFn" style="margin-left: 33px;" v-if="copyButton">复制
            </el-button>
            <el-button icon="yx-folder-upload" @click="exportInfoFn">导出</el-button>
          </el-col>
        </el-row>
* */

  // 注册组件
  vue.component(name, {
    template: '<div>\
    <slot name="selectParams">\
    <yu-input :size="size"  :placeholder="placeholder"  @blur="changeParamValue" :maxlength="maxlength"  :disabled="disabled"\
    icon="more" :on-icon-click="onIconClickFn" v-model="selectVal">\
    </yu-input>\
    </slot>\
    <yu-dialog :title="selectTit" :visible.sync="dialogVisible" :max-height="height" max-width="600">\
     <el-row :gutter="20">  <el-col :span="20"> <el-input v-model="use.paramName" placeholder="参数名称" style="width: 200px;"></el-input> \
      <el-button icon="yx-search" type="primary"   @click="seachFn">搜索</el-button>\
      <el-button icon="check"    type="primary"  @click="confirmFn">确定</el-button>\
       </el-col> \
     </el-row > \
    </el-form-q>\
    <el-table-x ref="paramsTable" :checkbox="use.checkbox" :data-url="use.dataUrl"  :table-columns="use.tableColumns"></el-table-x>\
    </yu-dialog>\
    </div>',
    props: {
      value: {
        type: String,
        required: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
	  maxlength:{
		  type: String,
		  required: false
	  },
      placeholder: {
        type: String,
        default: '公共宏参数选择'
      },
      params: Object,
      size: String
    },

    data: function () {
      return this.creatData();
    },

    mounted() {
		this.addEnd = this.params.addEnd || false;
      if (this.params.paramSelectValue != null && this.params.paramSelectValue != undefined) {
        this.selectVal = this.params.paramSelectValue;
      }
    },

    methods: {
      creatData: function () {
        var _this = this;
		//这里给一个默认值，如果value有值就默认写进
        var temp = _this.getDefaultData();
		temp.selectVal = _this.value;  //这里主要修改事件管理点击文件清空的问题
        yufp.clone(_this.params, temp);
        return temp;
      },
      getDefaultData: function () {
        var _this = this;
        return {
          dialogVisible: false,
          lableWidth: '80px',
          height: '300px',
          width: '400px',
          selectVal: '',
          selectTit: '公共宏参数选择',
          appCode:"",
		  addEnd:false,//是否直接追加

          use: {
            checkbox: false,
            fieldColumns: 3,
            paramName:"",
            baseParams:{
              condition:{}
            },
            fieldData: [{
              placeholder: '参数名称',
              field: 'paramName',
              type: 'input'
            }],
            buttons: [
              {
                label: '搜索',
                op: 'submit',
                type: 'primary',
                icon: 'search',
                click: function (model, valid) {
                  if (valid) {
                    var param = { condition: JSON.stringify(model) };
                    _this.$refs.paramsTable.remoteData(param);
                  }
                }
              },
              { label: '确定', op: 'submit', type: 'primary', icon: 'check',
               click:function(model,valid){
                _this.confirmFn();
               }}
            ],
            dataUrl: 'api/use/plats/paramValue/queryMacrosList',
            tableColumns: [
              { label: '参数名称', prop: 'paramName', resizable: true },
              { label: '参数代码', prop: 'paramCode', resizable: true }
            ],
            dataPrams: []
          }
        };
      },

      changeParamValue:function(){
        var _this = this;
        _this.$emit('input', _this.selectVal);
      },

      onIconClickFn: function () {
        var _this = this;
        _this.dialogVisible = true;

        _this.$nextTick(function (){
            _this.$refs.paramsTable.remoteData(  { condition: {appCode:_this.appCode}});

        });
      },
      queryFn: function () {
        var temp = this.params;
        temp.paramName = temp.paramName == '' || temp.paramName == undefined ? this.user.dataParams.paramName : temp.paramName;
        this.$refs.paramsTable.remoteData(temp);
      },

      seachFn: function () {
        this.$refs.paramsTable.remoteData({condition:{paramName:this.use.paramName,appCode:this.appCode}});
      },


      confirmFn: function () {
        var _this = this;
        var data = [];
        // if (_this.$refs.paramsTable.selections.length != 0) {
        if (_this.$refs.paramsTable.selections != undefined) {
          data = _this.$refs.paramsTable.selections;
        } else {
          data = _this.paramName;
        }
        if (data.length == 0) {
          _this.$message('请先选择一条数据');
          return;
        }
        _this.$emit('input',  data[0].paramCode);
        _this.$emit('select-fn', data,_this.value);
        _this.$nextTick(function () {
			if(_this.addEnd){
				_this.selectVal =  _this.selectVal +"{#"+data[0].paramCode+ "#}";
			}else{
				_this.selectVal =  "{#"+data[0].paramCode+ "#}";
				
			}
			_this.changeParamValue();
        });
        _this.dialogVisible = false;
      },
      array2String: function (array) {
        var paramsObj = {};
        var paramName = '', paramCodes = '';
        for (var i = 0; i < array.length; i++) {
          if (i == 0) {
            paramName = array[i].paramName;
            paramCodes = array[i].paramCode;
          } else {
            paramName = ',' + array[i].paramName;
            paramCodes = ',' + array[i].paramCode;
          }
        }
        paramsObj.paramCode = paramCodes;
        paramsObj.paramName = paramName;
        return paramsObj;
      }
    },


    watch: {


      params: {
        handler: function (val) {
          var _this =this;

          _this.appCode = val.appCode;

        /*  if(val.paramSelectValue!=null){
            _this.selectVal = val.paramSelectValue;

          }*/

         // _this.dialogVisible =true;

         /* _this.$nextTick(function (){
           // console.log(_this.$refs.paramsTable);
            if(_this.$refs.paramsTable!=null){
              _this.$refs.paramsTable.remoteData(param);
            }
           /!* _this.$nextTick(function(){
              //_this.$refs.paramsTable.remoteData(param);
            });*!/

          });*/
          yufp.clone(val, this);
        }
      }
    }
  });
}(Vue, yufp.$, 'yufp-select-params'));